// pages/detail/detail.js
var app = getApp()
let api = require('../../../api/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: [{
      name: "我的资产",
      index: "0"
    }, {
      name: "渠道资产",
      index: "1"
    }, {
      name: "订单明细",
      index: "2"
    }],
    order_navbar: [{
        name: '提现中',
      },
      {
        name: '提现完成',
      }
    ],
    currentTabsIndex: 0, // 我的资产/返佣管理/订单明细 初始值下标
    order_currentTabsIndex: 0, //    待返佣/已返佣 初始值下标
    display: '',
    // level:app.globalData.userInerInfo.level
    hidden:true
  },
  // 问号
  wenhao:function(){
    wx.showModal({
      title: '提示',
      content: '提现由九佰爺直接付款给渠道，您不需要处理',
      showCancel: false,
      success(res) { }
    })
  },
  // 我的资产/返佣管理/订单明细  tab 切换
  navbar: function(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    if (index == 0) {
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      api.property(param, res => {
        console.log(res)
        if (res.data.result.card != null){
          var length = res.data.result.card.card.length
          res.data.result.card.card = res.data.result.card.card.substr(0, 4) + "******" + res.data.result.card.card.substr(length - 4)
        }

        this.setData({
          indexData: res.data.result
        })
      })
    }
    if (index == 1) {
      var content = []
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      api.rebate(param, res => {
        console.log(res)
        var result = res.data.result
        var content = []
        if (this.data.order_currentTabsIndex == 0){
          result.profit.list.forEach(function (element) {
            if (element.status == '1') {
              content.push(element)
            }
          })
        } else if (this.data.order_currentTabsIndex == 1){
          result.profit.list.forEach(function (element) {
            if (element.status == '2') {
              content.push(element)
            }
          })
        }
        
        console.log(content)
        this.setData({
          content: content,
          result: result,
          rebateList: result.profit.list
        })
      })
    }
    if (index == 2) {
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      param.type = 2
      api.userorder(param, res => {
        console.log(res)
        res.data.result.forEach(function (element) {
          element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
        })
        this.setData({
          indexData: res.data.result
        })
      })
    }
    this.setData({
      currentTabsIndex: index
    })
  },
  // 待返佣/已返佣  tab 切换
  order_navbar: function(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    var list = this.data.rebateList
    console.log(list)
    var content = []
    if (index == 0) {
      console.log("----0----")
      list.forEach(function(element) {
        if (element.status == '1') {
          content.push(element)
        }
      })
    } else if (index == 1) {
      console.log("----1----")
      list.forEach(function(element) {
        if (element.status == '2') {
          content.push(element)
        }
      })
    }
    this.setData({
      order_currentTabsIndex: index,
      content: content
    })
  },
  rebate_detail: function(e) {
    var id = e.currentTarget.dataset.id
    var p_id = e.currentTarget.dataset.p_id
    var style = e.currentTarget.dataset.style
    // var p_id = app.globalData.userInerInfo.id
    wx.navigateTo({
      url: '../rebate_detail/rebate_detail?id=' + id + '&p_id=' + p_id + '&style=' + style,
    })
  },
  jiaoyi: function() {
    var id = app.globalData.userInerInfo.id
    var mobile = app.globalData.userInerInfo.number
    // var index = parseInt(this.data.currentTabsIndex) + 1
    if (this.data.currentTabsIndex == 0){
      var index = 0
    } else if (this.data.currentTabsIndex == 1){
      var index = 2
    }
    wx.navigateTo({
      url: '../jiaoyi/jiaoyi?id=' + id + "&mobile=" + mobile + "&index=" + index,
    })
  },

  phoneCall: function(param) {
    if (this.data.indexData.list.profit == 0) {
      wx.showModal({
        title: '提示',
        content: '可提现金额为0，不能提现！',
        showCancel: false,
        success(res) {}
      })
    } else {
      wx.navigateTo({
        url: '/pages/tixian/tixian?price=' + this.data.indexData.list.profit,
      })
      
    }

  },
  phoneCall1: function() {
    wx.showModal({
      title: '提示',
      content: '您还未绑定银行卡',
      showCancel: false,
      success(res) {}
    })
  },

  showview: function() {
    this.setData({
      display: "block"
    })
  },
  hideview: function() {
    this.setData({
      display: "none"
    })
  },
  bank: function() {
    var id = app.globalData.userInerInfo.id
    var mobile = app.globalData.userInerInfo.number
    wx.navigateTo({
      url: '/pages/A/bank/bank?id=' + id + "&mobile=" + mobile,
      // url: '/pages/A/bank/bank'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(data) {
    console.log(app.globalData.userInerInfo)
    this.setData({
      data: data
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showLoading({
      title: '加载中',
    })
    var data = this.data.data
    if(data.index == 0){
      api.property(data, res => {
        console.log(res)
        if (res.data.result.card != null) {
          var length = res.data.result.card.card.length
          res.data.result.card.card = res.data.result.card.card.substr(0, 4) + "******" + res.data.result.card.card.substr(length - 4)
        }
        this.setData({
          indexData: res.data.result,
          data: data,
          level: app.globalData.userInerInfo.level,
          hidden:false
        })
        wx.hideLoading()
      })
    }else if(data.index == 1){
      if (app.globalData.userInerInfo.level == 0) {
        var content = []
        var param = {}
        param.id = this.data.data.id
        param.mobile = this.data.data.mobile
        api.rebate(param, res => {
          console.log(res)
          var result = res.data.result
          var content = []
          result.profit.list.forEach(function (element) {
            if (element.status == '1') {
              content.push(element)
            }
          })
          console.log(content)
          this.setData({
            content: content,
            result: result,
            rebateList: result.profit.list,
            level: app.globalData.userInerInfo.level,
            currentTabsIndex:1,
            hidden: false
          })
          wx.hideLoading()
        })
      }
    } else if (data.index == 2){
      console.log(data)
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      param.type = 2
      api.userorder(param, res => {
        console.log(res)
        res.data.result.forEach(function (element) {
            element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
        })
        this.setData({
          indexData: res.data.result,
          level: app.globalData.userInerInfo.level,
          currentTabsIndex: 2,
          hidden: false
        })
        wx.hideLoading()
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var index = this.data.currentTabsIndex
    if (index == 0) {
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      api.property(param, res => {
        console.log(res)
        if (res.data.result.card != null) {
          var length = res.data.result.card.card.length
          res.data.result.card.card = res.data.result.card.card.substr(0, 4) + "******" + res.data.result.card.card.substr(length - 4)
        }
        this.setData({
          indexData: res.data.result
        })
        wx.stopPullDownRefresh()
      })
    }
    if (index == 1) {
      var content = []
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      api.rebate(param, res => {
        console.log(res)
        var result = res.data.result
        var content = []
        if (this.data.order_currentTabsIndex == 0) {
          result.profit.list.forEach(function (element) {
            if (element.status == '1') {
              content.push(element)
            }
          })
        } else if (this.data.order_currentTabsIndex == 1) {
          result.profit.list.forEach(function (element) {
            if (element.status == '2') {
              content.push(element)
            }
          })
        }

        console.log(content)
        this.setData({
          content: content,
          result: result,
          rebateList: result.profit.list
        })
        wx.stopPullDownRefresh()
      })
    }
    if (index == 2) {
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      param.type = 2
      api.userorder(param, res => {
        console.log(res)
        res.data.result.forEach(function (element) {
          element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
        })
        this.setData({
          indexData: res.data.result
        })
      })
      wx.stopPullDownRefresh()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})